package com.lpc.android.abc.base.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.TintTypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.lpc.android.abc.base.R;
import com.lpc.android.abc.base.utils.ActivityStackManager;

public class TopNavBar extends FrameLayout {


    private LayoutInflater mInflater;

    private View mView;
    private TextView mTextTitle;
    private EditText mSearchView;
    private Button mRightButton;
    private ImageView mLeftButton;
    private ImageButton mRightImgBtn;


    public TopNavBar(Context context) {
        this(context, null);
    }

    public TopNavBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TopNavBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);


        initView();
//        setContentInsetsRelative(10, 10);


        if (attrs != null) {
            final TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(), attrs,
                    R.styleable.TopNavBar, defStyleAttr, 0);

            boolean isShowLeftIcon = a.getBoolean(R.styleable.TopNavBar_isShowLeftIcon, false);
            if (isShowLeftIcon) {
                setLeftButtonText();
            }

            CharSequence title = a.getText(R.styleable.TopNavBar_title);
            if (title != null) {
                setTitle(title);
            }

            final Drawable rightIcon = a.getDrawable(R.styleable.TopNavBar_rightButtonIcon);
            if (rightIcon != null) {
                //setNavigationIcon(navIcon);
                setRightButtonIcon(rightIcon);
            }

            CharSequence rightButtonText = a.getText(R.styleable.TopNavBar_rightButtonText);
            if (rightButtonText != null) {
                setRightButtonText(rightButtonText);
            }

            boolean isShowSearchView = a.getBoolean(R.styleable.TopNavBar_isShowSearchView, false);
            if (isShowSearchView) {
                showSearchView();
                hideTitleView();
            }

            a.recycle();
        }

    }

    private void setLeftButtonText() {

        if (mLeftButton != null) {

            mLeftButton.setVisibility(VISIBLE);
            mLeftButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    ActivityStackManager.getInstance().getTopActivity().finish();
                }
            });
        }
    }

    private void initView() {


        if (mView == null) {

            mInflater = LayoutInflater.from(getContext());
            mView = mInflater.inflate(R.layout.toolbar, null);


            mTextTitle = (TextView) mView.findViewById(R.id.toolbar_title);
            mSearchView = (EditText) mView.findViewById(R.id.toolbar_searchview);
            mRightButton = (Button) mView.findViewById(R.id.toolbar_rightButton);
            mRightImgBtn = (ImageButton) mView.findViewById(R.id.toolbar_rightImgBtn);
            mLeftButton = (ImageView) mView.findViewById(R.id.toolbar_leftButton);


            LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);

            addView(mView, lp);
        }


    }


    public void setRightButtonIcon(Drawable icon) {

        if (mRightImgBtn != null) {

//            mRightImgBtn.setBackground(icon);
            mRightImgBtn.setImageDrawable(icon);
            mRightImgBtn.setVisibility(VISIBLE);
        }

    }

    public void setRightButtonIcon(int icon) {

        setRightButtonIcon(getResources().getDrawable(icon));
    }


    public void setRightButtonOnClickListener(OnClickListener li) {

        if(mRightButton != null){
            mRightButton.setOnClickListener(li);
        }

        if(mRightImgBtn != null){
            mRightImgBtn.setOnClickListener(li);
        }

    }

    public void setLeftButtonOnClickListener(OnClickListener li) {

        mLeftButton.setOnClickListener(li);
    }

    public void setRightButtonText(CharSequence text) {
        mRightButton.setText(text);
        mRightButton.setVisibility(VISIBLE);
    }

    public void setRightButtonText(int id) {
        setRightButtonText(getResources().getString(id));
    }


    public Button getRightButton() {

        return this.mRightButton;
    }


    public void setTitle(CharSequence title) {

        if (mTextTitle != null) {
            mTextTitle.setText(title);
            showTitleView();
        }


    }


    public void showSearchView() {

        if (mSearchView != null)
            mSearchView.setVisibility(VISIBLE);

    }


    public void hideSearchView() {
        if (mSearchView != null)
            mSearchView.setVisibility(GONE);
    }

    public void showTitleView() {
        if (mTextTitle != null)
            mTextTitle.setVisibility(VISIBLE);
    }


    public void hideTitleView() {
        if(mLeftButton != null)
            mLeftButton.setVisibility(GONE);
        if(mTextTitle != null)
            mTextTitle.setVisibility(GONE);
        if(mRightButton != null)
            mRightButton.setVisibility(GONE);
        if(mRightImgBtn != null)
            mRightImgBtn.setVisibility(GONE);
    }


//
//    private void ensureRightButtonView() {
//        if (mRightImageButton == null) {
//            mRightImageButton = new ImageButton(getContext(), null,
//                    android.support.v7.appcompat.R.attr.toolbarNavigationButtonStyle);
//            final LayoutParams lp = generateDefaultLayoutParams();
//            lp.gravity = GravityCompat.START | (Gravity.VERTICAL_GRAVITY_MASK);
//            mRightImageButton.setLayoutParams(lp);
//        }
//    }


}
